package net.hn.hnms.biz.mine.controller;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.hn.hnms.biz.api.safety.location.fegin.RemoteLocationService;
import net.hn.hnms.biz.mine.domain.MineCodeMapPo;
import net.hn.hnms.biz.mine.domain.dto.*;
import net.hn.hnms.biz.mine.domain.dto.*;
import net.hn.hnms.biz.mine.domain.vo.OtherApiAqjkNowVo;
import net.hn.hnms.biz.mine.domain.vo.OtherApiJxryNowVO;
import net.hn.hnms.biz.mine.mapper.MineCodeMapMapper;
import net.hn.hnms.biz.mine.service.OtherSystemApiService;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.redis.domain.SysOrganizationVo;
import net.hn.hnms.sys.common.redis.utils.RedisUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("OtherSystemApi")
@Api(value = "其他系统api请求", tags = {"其他系统api请求"})
public class OtherSystemApi {

    @Resource
    private MineCodeMapMapper mineCodeMapMapper;

    @Resource
    private RemoteLocationService remoteLocationService;

    //本层接口服务
    @Resource
    private OtherSystemApiService otherSystemApiService;
    @ApiOperation("获取龙软-安全监测实时概况（公司）")
    @GetMapping("getOtherSystemCompanyAqjkNowApi")
    public List<OtherApiAqjkNowVo> getOtherSystemCompanyAqjkNowApi(@ApiParam(value = "公司编码") String companyCode) {
        try {
            MineDataResponse<List<OtherApiAqjkNowDto>> listMineDataResponse = sendGetRequest("http://10.103.9.31/pddP/PDDPAPI/GetRealtimeoverview", "", new TypeReference<MineDataResponse<List<OtherApiAqjkNowDto>>>() {});

//        String responseData = "{ \"success\": true, \"data\": [ { \"MineID\": \"10100102901\", \"MineName\": \"青岗坪煤矿\", \"SensorCount\": 171, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"10100102902\", \"MineName\": \"西川煤矿\", \"SensorCount\": 209, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"10100109103\", \"MineName\": \"柳巷煤矿\", \"SensorCount\": 137, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"10105087403\", \"MineName\": \"雨汪煤矿一井\", \"SensorCount\": 374, \"WarnCount\": 310, \"CoWarnCount\": 13, \"CH4WarnCount\": 63 }, { \"MineID\": \"10105087401\", \"MineName\": \"白龙山煤矿一井\", \"SensorCount\": 275, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"10103047001\", \"MineName\": \"灵泉矿\", \"SensorCount\": 153, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"10103047003\", \"MineName\": \"铁北矿\", \"SensorCount\": 112, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"10103047006\", \"MineName\": \"灵东矿\", \"SensorCount\": 207, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"10103047007\", \"MineName\": \"灵露矿\", \"SensorCount\": 138, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"01002004001\", \"MineName\": \"高头窑煤矿\", \"SensorCount\": 0, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272502\", \"MineName\": \"华亭煤矿\", \"SensorCount\": 204, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272501\", \"MineName\": \"砚北煤矿\", \"SensorCount\": 341, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272503\", \"MineName\": \"陈家沟煤矿\", \"SensorCount\": 244, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272504\", \"MineName\": \"东峡煤矿\", \"SensorCount\": 217, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272507\", \"MineName\": \"山寨煤矿\", \"SensorCount\": 160, \"WarnCount\": 4, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272505\", \"MineName\": \"马蹄沟煤矿\", \"SensorCount\": 74, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272508\", \"MineName\": \"新窑公司\", \"SensorCount\": 162, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272509\", \"MineName\": \"新柏公司\", \"SensorCount\": 164, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272511\", \"MineName\": \"大柳煤矿\", \"SensorCount\": 182, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272512\", \"MineName\": \"赤城煤矿\", \"SensorCount\": 190, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"01001002013\", \"MineName\": \"核桃峪煤矿\", \"SensorCount\": 230, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"01001002014\", \"MineName\": \"新庄煤矿\", \"SensorCount\": 140, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"01001002015\", \"MineName\": \"刘园子煤矿\", \"SensorCount\": 84, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 } ], \"message\": \"成功\" }";
//        ObjectMapper objectMapper = new ObjectMapper();
//        MineDataResponse<List<OtherApiAqjkNowDto>> mineDataResponse = objectMapper.readValue(responseData.toString(), new TypeReference<MineDataResponse<List<OtherApiAqjkNowDto>>>(){});
        //获取龙软的所有煤矿数据
        List<OtherApiAqjkNowDto> mineDataList = listMineDataResponse.getData();
        //将数据根据龙然煤矿id转成map
        Map<String, OtherApiAqjkNowDto> mineDataMap = new HashMap<>();
        for (OtherApiAqjkNowDto dto : mineDataList) {
            mineDataMap.put(dto.getMineID(), dto);
        }

        //获取映射表
        List<MineCodeMapPo> mineCodeMapPos = mineCodeMapMapper.selectList(null);
        Map<String, MineCodeMapPo> mineCodeMap = new HashMap<>();
        for (MineCodeMapPo po : mineCodeMapPos) {
            mineCodeMap.put(po.getMineCode(), po);
        }
        //获取公司下所有煤矿
        SysOrganizationVo sysOrganizationVos = RedisUtils.getCacheObject(Constants.SYS_CODESUB_KEY+companyCode);
        if (sysOrganizationVos == null) {
            new HuaNengException(Constants.FAIL, "未查询到该公司编码" + companyCode);
        }
        List<SysOrganizationVo> subCodeList = sysOrganizationVos.getSubCodeList();
        List<OtherApiAqjkNowVo> collect = subCodeList.stream().map(t -> {
            OtherApiAqjkNowVo otherApiAqjkNowVo = new OtherApiAqjkNowVo();
            otherApiAqjkNowVo.setMineCode(t.getOrgCode());
            otherApiAqjkNowVo.setMineName(t.getOrgName());
            //根据获取煤矿编码映射关系
            if (mineCodeMap.get(t.getOrgCode()) != null) {
                MineCodeMapPo mineCodeMapPo = mineCodeMap.get(t.getOrgCode());
                //判断是否有龙软id
                if (mineCodeMapPo.getMineId() != null) {
                    //获取龙软煤矿id数据
                    OtherApiAqjkNowDto otherApiAqjkNowDto = mineDataMap.get(mineCodeMapPo.getMineId());
                    //判断龙软数据是否存在
                    if (otherApiAqjkNowDto != null) {
                        //有龙软数据
                        //井下探头总数
                        otherApiAqjkNowVo.setSensorCount(String.valueOf(otherApiAqjkNowDto.getSensorCount()));
                        //报警总数
                        otherApiAqjkNowVo.setWarnCount(String.valueOf(otherApiAqjkNowDto.getWarnCount()));
                        //一氧化碳报警总数
                        otherApiAqjkNowVo.setCoWarnCount(String.valueOf(otherApiAqjkNowDto.getCoWarnCount()));
                        //甲烷报警总数
                        otherApiAqjkNowVo.setCH4WarnCount(String.valueOf(otherApiAqjkNowDto.getCH4WarnCount()));
                        otherApiAqjkNowVo.setRemark("数据来自龙软：" + otherApiAqjkNowDto.getMineID());
                    } else {
                        //没有龙软数据

                        //获取华能井下人数
                        try {

                            //远程openfein调用华能接口
                            //井下探头总数
                            otherApiAqjkNowVo.setSensorCount(String.valueOf(0));
                            //报警总数
                            otherApiAqjkNowVo.setWarnCount(String.valueOf(0));
                            //一氧化碳报警总数
                            otherApiAqjkNowVo.setCoWarnCount(String.valueOf(0));
                            //甲烷报警总数
                            otherApiAqjkNowVo.setCH4WarnCount(String.valueOf(0));
                            otherApiAqjkNowVo.setRemark("数据来自华能：" + t.getOrgCode());
                        } catch (RuntimeException e) {
                            //井下探头总数
                            otherApiAqjkNowVo.setSensorCount(String.valueOf(0));
                            //报警总数
                            otherApiAqjkNowVo.setWarnCount(String.valueOf(0));
                            //一氧化碳报警总数
                            otherApiAqjkNowVo.setCoWarnCount(String.valueOf(0));
                            //甲烷报警总数
                            otherApiAqjkNowVo.setCH4WarnCount(String.valueOf(0));
                            otherApiAqjkNowVo.setRemark("fein获取华能数据失败"+e.getMessage());
                        }
                    }
                } else {
                    //没有龙软id  直接查询华能数据
                    try {
                        //井下探头总数
                        otherApiAqjkNowVo.setSensorCount(String.valueOf(0));
                        //报警总数
                        otherApiAqjkNowVo.setWarnCount(String.valueOf(0));
                        //一氧化碳报警总数
                        otherApiAqjkNowVo.setCoWarnCount(String.valueOf(0));
                        //甲烷报警总数
                        otherApiAqjkNowVo.setCH4WarnCount(String.valueOf(0));
                        otherApiAqjkNowVo.setRemark("数据来自华能：" + t.getOrgCode());
                    } catch (RuntimeException e) {
                        //井下探头总数
                        otherApiAqjkNowVo.setSensorCount(String.valueOf(0));
                        //报警总数
                        otherApiAqjkNowVo.setWarnCount(String.valueOf(0));
                        //一氧化碳报警总数
                        otherApiAqjkNowVo.setCoWarnCount(String.valueOf(0));
                        //甲烷报警总数
                        otherApiAqjkNowVo.setCH4WarnCount(String.valueOf(0));
                        otherApiAqjkNowVo.setRemark("fein获取华能数据失败"+e.getMessage());
                    }

                }
            }
            return otherApiAqjkNowVo;
        }).collect(Collectors.toList());
        return collect;
    } catch (IOException e) {
        e.printStackTrace();
    }
        return null;
    }
    @ApiOperation("获取龙软-安全监测实时概况（集团）")
    @GetMapping("getOtherSystemGroupAqjkNowApi")
    public List<OtherApiAqjkNowVo> getOtherSystemCompanyAqjkNowApi() {
        try {
            MineDataResponse<List<OtherApiAqjkNowDto>> listMineDataResponse = sendGetRequest("http://10.103.9.31/pddP/PDDPAPI/GetRealtimeoverview", "", new TypeReference<MineDataResponse<List<OtherApiAqjkNowDto>>>() {});

//            String responseData = "{ \"success\": true, \"data\": [ { \"MineID\": \"10100102901\", \"MineName\": \"青岗坪煤矿\", \"SensorCount\": 171, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"10100102902\", \"MineName\": \"西川煤矿\", \"SensorCount\": 209, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"10100109103\", \"MineName\": \"柳巷煤矿\", \"SensorCount\": 137, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"10105087403\", \"MineName\": \"雨汪煤矿一井\", \"SensorCount\": 374, \"WarnCount\": 310, \"CoWarnCount\": 13, \"CH4WarnCount\": 63 }, { \"MineID\": \"10105087401\", \"MineName\": \"白龙山煤矿一井\", \"SensorCount\": 275, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"10103047001\", \"MineName\": \"灵泉矿\", \"SensorCount\": 153, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"10103047003\", \"MineName\": \"铁北矿\", \"SensorCount\": 112, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"10103047006\", \"MineName\": \"灵东矿\", \"SensorCount\": 207, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"10103047007\", \"MineName\": \"灵露矿\", \"SensorCount\": 138, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"01002004001\", \"MineName\": \"高头窑煤矿\", \"SensorCount\": 0, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272502\", \"MineName\": \"华亭煤矿\", \"SensorCount\": 204, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272501\", \"MineName\": \"砚北煤矿\", \"SensorCount\": 341, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272503\", \"MineName\": \"陈家沟煤矿\", \"SensorCount\": 244, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272504\", \"MineName\": \"东峡煤矿\", \"SensorCount\": 217, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272507\", \"MineName\": \"山寨煤矿\", \"SensorCount\": 160, \"WarnCount\": 4, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272505\", \"MineName\": \"马蹄沟煤矿\", \"SensorCount\": 74, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272508\", \"MineName\": \"新窑公司\", \"SensorCount\": 162, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272509\", \"MineName\": \"新柏公司\", \"SensorCount\": 164, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272511\", \"MineName\": \"大柳煤矿\", \"SensorCount\": 182, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"62272512\", \"MineName\": \"赤城煤矿\", \"SensorCount\": 190, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"01001002013\", \"MineName\": \"核桃峪煤矿\", \"SensorCount\": 230, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"01001002014\", \"MineName\": \"新庄煤矿\", \"SensorCount\": 140, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 }, { \"MineID\": \"01001002015\", \"MineName\": \"刘园子煤矿\", \"SensorCount\": 84, \"WarnCount\": 0, \"CoWarnCount\": 0, \"CH4WarnCount\": 0 } ], \"message\": \"成功\" }";
//            ObjectMapper objectMapper = new ObjectMapper();
//            MineDataResponse<List<OtherApiAqjkNowDto>> mineDataResponse = objectMapper.readValue(responseData.toString(), new TypeReference<MineDataResponse<List<OtherApiAqjkNowDto>>>(){});
            //获取龙软的所有煤矿数据
            List<OtherApiAqjkNowDto> mineDataList = listMineDataResponse.getData();
            //将数据根据龙然煤矿id转成map
            Map<String, OtherApiAqjkNowDto> mineDataMap = new HashMap<>();
            for (OtherApiAqjkNowDto dto : mineDataList) {
                mineDataMap.put(dto.getMineID(), dto);
            }

            //获取映射表
            List<MineCodeMapPo> mineCodeMapPos = mineCodeMapMapper.selectList(null);
            Map<String, MineCodeMapPo> mineCodeMap = new HashMap<>();
            for (MineCodeMapPo po : mineCodeMapPos) {
                mineCodeMap.put(po.getMineCode(), po);
            }
            SysOrganizationVo sysOrganizationVos = RedisUtils.getCacheObject(Constants.SYS_CODESUB_KEY+0);
            if (sysOrganizationVos == null) {
                new HuaNengException(Constants.FAIL, "未查询到集团信息");
            }
            //获取集团下所有公司
            List<SysOrganizationVo> subCodeList = sysOrganizationVos.getSubCodeList();
            List<OtherApiAqjkNowVo> otherApiAqjkNowVOAll = new ArrayList<>();
            //遍历公司
            for (SysOrganizationVo sysOrganizationVo : subCodeList) {
                List<OtherApiAqjkNowVo> collect = sysOrganizationVo.getSubCodeList().stream().map(t -> {
                    OtherApiAqjkNowVo otherApiAqjkNowVo = new OtherApiAqjkNowVo();
                    otherApiAqjkNowVo.setMineCode(t.getOrgCode());
                    otherApiAqjkNowVo.setMineName(t.getOrgName());
                    //根据获取煤矿编码映射关系
                    if (mineCodeMap.get(t.getOrgCode()) != null) {
                        MineCodeMapPo mineCodeMapPo = mineCodeMap.get(t.getOrgCode());
                        //判断是否有龙软id
                        if (mineCodeMapPo.getMineId() != null) {
                            //获取龙软煤矿id数据
                            OtherApiAqjkNowDto otherApiAqjkNowDto = mineDataMap.get(mineCodeMapPo.getMineId());
                            //判断龙软数据是否存在
                            if (otherApiAqjkNowDto != null) {
                                //有龙软数据
                                //井下探头总数
                                otherApiAqjkNowVo.setSensorCount(String.valueOf(otherApiAqjkNowDto.getSensorCount()));
                                //报警总数
                                otherApiAqjkNowVo.setWarnCount(String.valueOf(otherApiAqjkNowDto.getWarnCount()));
                                //一氧化碳报警总数
                                otherApiAqjkNowVo.setCoWarnCount(String.valueOf(otherApiAqjkNowDto.getCoWarnCount()));
                                //甲烷报警总数
                                otherApiAqjkNowVo.setCH4WarnCount(String.valueOf(otherApiAqjkNowDto.getCH4WarnCount()));
                                otherApiAqjkNowVo.setRemark("数据来自龙软：" + otherApiAqjkNowDto.getMineID());
                            } else {
                                //没有龙软数据
                                //获取华能井下人数
                                try {

                                    //远程openfein调用华能接口
                                    //井下探头总数
                                    otherApiAqjkNowVo.setSensorCount(String.valueOf(0));
                                    //报警总数
                                    otherApiAqjkNowVo.setWarnCount(String.valueOf(0));
                                    //一氧化碳报警总数
                                    otherApiAqjkNowVo.setCoWarnCount(String.valueOf(0));
                                    //甲烷报警总数
                                    otherApiAqjkNowVo.setCH4WarnCount(String.valueOf(0));
                                    otherApiAqjkNowVo.setRemark("数据来自龙软：" + otherApiAqjkNowDto.getMineID());
                                } catch (RuntimeException e) {
                                    //井下探头总数
                                    otherApiAqjkNowVo.setSensorCount(String.valueOf(0));
                                    //报警总数
                                    otherApiAqjkNowVo.setWarnCount(String.valueOf(0));
                                    //一氧化碳报警总数
                                    otherApiAqjkNowVo.setCoWarnCount(String.valueOf(0));
                                    //甲烷报警总数
                                    otherApiAqjkNowVo.setCH4WarnCount(String.valueOf(0));
                                    otherApiAqjkNowVo.setRemark("fein获取华能数据失败"+e.getMessage());
                                }
                            }
                        } else {
                            //没有龙软id  直接查询华能数据
                            try {
                                //井下探头总数
                                otherApiAqjkNowVo.setSensorCount(String.valueOf(0));
                                //报警总数
                                otherApiAqjkNowVo.setWarnCount(String.valueOf(0));
                                //一氧化碳报警总数
                                otherApiAqjkNowVo.setCoWarnCount(String.valueOf(0));
                                //甲烷报警总数
                                otherApiAqjkNowVo.setCH4WarnCount(String.valueOf(0));
                                otherApiAqjkNowVo.setRemark("数据来自华能：" + t.getOrgCode());
                            } catch (RuntimeException e) {
                                //井下探头总数
                                otherApiAqjkNowVo.setSensorCount(String.valueOf(0));
                                //报警总数
                                otherApiAqjkNowVo.setWarnCount(String.valueOf(0));
                                //一氧化碳报警总数
                                otherApiAqjkNowVo.setCoWarnCount(String.valueOf(0));
                                //甲烷报警总数
                                otherApiAqjkNowVo.setCH4WarnCount(String.valueOf(0));
                                otherApiAqjkNowVo.setRemark("fein获取华能数据失败"+e.getMessage());
                            }
                        }
                    }
                    return otherApiAqjkNowVo;
                }).collect(Collectors.toList());
                    collect.forEach(t->{
                        otherApiAqjkNowVOAll.add(t);
                    });
            }
            return otherApiAqjkNowVOAll;
            } catch (IOException e) {
                e.printStackTrace();
            }
        return null;
    }
    @ApiOperation("获取龙软-人员定位实时概况（公司）")
    @GetMapping("getOtherSystemCompanyJxryNowApi")
    public List<OtherApiJxryNowVO> getOtherSystemCompanyJxryNowApi(@ApiParam(value = "公司编码") String companyCode) {
        try {
            //井下实时总人数(各矿一条统计数据)
            //http://10.103.9.31/pddP/PDDPAPI/NumInWell
            MineDataResponse<List<OtherApiJxryNowDto>> listMineDataResponse = sendGetRequest("http://10.103.9.31/pddP/PDDPAPI/NumInWell", "", new TypeReference<MineDataResponse<List<OtherApiJxryNowDto>>>() {});

//            String responseData = "{ \"success\": true, \"data\": [ { \"MineID\": \"62272508\", \"MineName\": \"新窑公司\", \"InWellNum\": 151 }, { \"MineID\": \"62272503\", \"MineName\": \"陈家沟煤矿\", \"InWellNum\": 141 }, { \"MineID\": \"62272505\", \"MineName\": \"马蹄沟煤矿\", \"InWellNum\": 43 }, { \"MineID\": \"62272502\", \"MineName\": \"华亭煤矿\", \"InWellNum\": 270 }, { \"MineID\": \"62272512\", \"MineName\": \"赤城煤矿\", \"InWellNum\": 120 }, { \"MineID\": \"62272509\", \"MineName\": \"新柏公司\", \"InWellNum\": 174 }, { \"MineID\": \"62272507\", \"MineName\": \"山寨煤矿\", \"InWellNum\": 182 }, { \"MineID\": \"62272501\", \"MineName\": \"砚北煤矿\", \"InWellNum\": 301 }, { \"MineID\": \"62272511\", \"MineName\": \"大柳煤矿\", \"InWellNum\": 195 }, { \"MineID\": \"62272504\", \"MineName\": \"东峡煤矿\", \"InWellNum\": 169 }, { \"MineID\": \"10103047007\", \"MineName\": \"灵露矿\", \"InWellNum\": 163 }, { \"MineID\": \"01001002013\", \"MineName\": \"核桃峪煤矿\", \"InWellNum\": 414 }, { \"MineID\": \"10103047001\", \"MineName\": \"灵泉矿\", \"InWellNum\": 207 }, { \"MineID\": \"10100102901\", \"MineName\": \"青岗坪煤矿\", \"InWellNum\": 175 }, { \"MineID\": \"01001002015\", \"MineName\": \"刘园子煤矿\", \"InWellNum\": 163 }, { \"MineID\": \"10103047006\", \"MineName\": \"灵东矿\", \"InWellNum\": 172 }, { \"MineID\": \"10100102902\", \"MineName\": \"西川煤矿\", \"InWellNum\": 167 }, { \"MineID\": \"10100109103\", \"MineName\": \"柳巷煤矿\", \"InWellNum\": 104 }, { \"MineID\": \"10103047003\", \"MineName\": \"铁北矿\", \"InWellNum\": 202 }, { \"MineID\": \"01001002014\", \"MineName\": \"新庄煤矿\", \"InWellNum\": 611 }, { \"MineID\": \"10105087401\", \"MineName\": \"白龙山煤矿一井\", \"InWellNum\": 341 } ], \"message\": \"成功获取各单位下井人数\" }";
//            ObjectMapper objectMapper = new ObjectMapper();
//            MineDataResponse<List<OtherApiJxryNowDto>> mineDataResponse = objectMapper.readValue(responseData.toString(), new TypeReference<MineDataResponse<List<OtherApiJxryNowDto>>>(){});
            //获取龙软的所有煤矿数据
            List<OtherApiJxryNowDto> mineDataList = listMineDataResponse.getData();
            //将数据根据龙然煤矿id转成map
            Map<String, OtherApiJxryNowDto> mineDataMap = new HashMap<>();
            for (OtherApiJxryNowDto dto : mineDataList) {
                mineDataMap.put(dto.getMineID(), dto);
            }

            //获取映射表
            List<MineCodeMapPo> mineCodeMapPos = mineCodeMapMapper.selectList(null);
            Map<String, MineCodeMapPo> mineCodeMap = new HashMap<>();
            for (MineCodeMapPo po : mineCodeMapPos) {
                mineCodeMap.put(po.getMineCode(), po);
            }
            //获取公司下所有煤矿
            SysOrganizationVo sysOrganizationVos = RedisUtils.getCacheObject(Constants.SYS_CODESUB_KEY+companyCode);
            if (sysOrganizationVos == null) {
                new HuaNengException(Constants.FAIL, "未查询到该公司编码" + companyCode);
            }
            List<SysOrganizationVo> subCodeList = sysOrganizationVos.getSubCodeList();
            List<OtherApiJxryNowVO> collect = subCodeList.stream().map(t -> {
                OtherApiJxryNowVO otherApiJxryNowVO = new OtherApiJxryNowVO();
                otherApiJxryNowVO.setMineCode(t.getOrgCode());
                otherApiJxryNowVO.setMineName(t.getOrgName());
                //根据获取煤矿编码映射关系
                if (mineCodeMap.get(t.getOrgCode()) != null) {
                    MineCodeMapPo mineCodeMapPo = mineCodeMap.get(t.getOrgCode());
                    //判断是否有龙软id
                    if (mineCodeMapPo.getMineId() != null) {
                        //获取龙软煤矿id数据
                        OtherApiJxryNowDto otherApiJxryNowDto = mineDataMap.get(mineCodeMapPo.getMineId());
                        //判断龙软数据是否存在
                        if (otherApiJxryNowDto != null) {
                            //有龙软数据
                            otherApiJxryNowVO.setInWellNum(String.valueOf(otherApiJxryNowDto.getInWellNum()));
                            otherApiJxryNowVO.setRemark("数据来自龙软：" + otherApiJxryNowDto.getMineID());
                        } else {
                            //没有龙软数据

                            //获取华能井下人数
                            try {
                                otherApiJxryNowVO.setInWellNum(remoteLocationService.getPitNumber(t.getOrgCode()));
                                otherApiJxryNowVO.setRemark("数据来自华能：" + t.getOrgCode());
                            } catch (RuntimeException e) {
                                otherApiJxryNowVO.setInWellNum("0");
                                otherApiJxryNowVO.setRemark("fein获取华能数据失败"+e.getMessage());
                            }
                        }
                    } else {
                        //没有龙软id  直接查询华能数据
                        try {
                            otherApiJxryNowVO.setInWellNum(remoteLocationService.getPitNumber(t.getOrgCode()));
                            otherApiJxryNowVO.setRemark("数据来自华能：" + t.getOrgCode());
                        } catch (RuntimeException e) {
                            otherApiJxryNowVO.setInWellNum("0");
                            otherApiJxryNowVO.setRemark("fein获取华能数据失败"+e.getMessage());
                        }
                    }
                }
                return otherApiJxryNowVO;
            }).collect(Collectors.toList());
            return collect;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    @ApiOperation("获取龙软-人员定位实时概况（集团）")
    @GetMapping("getOtherSystemGroupJxryNowApi")
    public List<OtherApiJxryNowVO> getOtherSystemGroupJxryNowApi() {
        try {
            //井下实时总人数(各矿一条统计数据)
            //http://10.103.9.31/pddP/PDDPAPI/NumInWell
            MineDataResponse<List<OtherApiJxryNowDto>> listMineDataResponse = sendGetRequest("http://10.103.9.31/pddP/PDDPAPI/NumInWell", "", new TypeReference<MineDataResponse<List<OtherApiJxryNowDto>>>() {});

//            String responseData = "{ \"success\": true, \"data\": [ { \"MineID\": \"62272508\", \"MineName\": \"新窑公司\", \"InWellNum\": 151 }, { \"MineID\": \"62272503\", \"MineName\": \"陈家沟煤矿\", \"InWellNum\": 141 }, { \"MineID\": \"62272505\", \"MineName\": \"马蹄沟煤矿\", \"InWellNum\": 43 }, { \"MineID\": \"62272502\", \"MineName\": \"华亭煤矿\", \"InWellNum\": 270 }, { \"MineID\": \"62272512\", \"MineName\": \"赤城煤矿\", \"InWellNum\": 120 }, { \"MineID\": \"62272509\", \"MineName\": \"新柏公司\", \"InWellNum\": 174 }, { \"MineID\": \"62272507\", \"MineName\": \"山寨煤矿\", \"InWellNum\": 182 }, { \"MineID\": \"62272501\", \"MineName\": \"砚北煤矿\", \"InWellNum\": 301 }, { \"MineID\": \"62272511\", \"MineName\": \"大柳煤矿\", \"InWellNum\": 195 }, { \"MineID\": \"62272504\", \"MineName\": \"东峡煤矿\", \"InWellNum\": 169 }, { \"MineID\": \"10103047007\", \"MineName\": \"灵露矿\", \"InWellNum\": 163 }, { \"MineID\": \"01001002013\", \"MineName\": \"核桃峪煤矿\", \"InWellNum\": 414 }, { \"MineID\": \"10103047001\", \"MineName\": \"灵泉矿\", \"InWellNum\": 207 }, { \"MineID\": \"10100102901\", \"MineName\": \"青岗坪煤矿\", \"InWellNum\": 175 }, { \"MineID\": \"01001002015\", \"MineName\": \"刘园子煤矿\", \"InWellNum\": 163 }, { \"MineID\": \"10103047006\", \"MineName\": \"灵东矿\", \"InWellNum\": 172 }, { \"MineID\": \"10100102902\", \"MineName\": \"西川煤矿\", \"InWellNum\": 167 }, { \"MineID\": \"10100109103\", \"MineName\": \"柳巷煤矿\", \"InWellNum\": 104 }, { \"MineID\": \"10103047003\", \"MineName\": \"铁北矿\", \"InWellNum\": 202 }, { \"MineID\": \"01001002014\", \"MineName\": \"新庄煤矿\", \"InWellNum\": 611 }, { \"MineID\": \"10105087401\", \"MineName\": \"白龙山煤矿一井\", \"InWellNum\": 341 } ], \"message\": \"成功获取各单位下井人数\" }";
//            ObjectMapper objectMapper = new ObjectMapper();
//            MineDataResponse<List<OtherApiJxryNowDto>> mineDataResponse = objectMapper.readValue(responseData.toString(), new TypeReference<MineDataResponse<List<OtherApiJxryNowDto>>>(){});
            //获取龙软的所有煤矿数据
            List<OtherApiJxryNowDto> mineDataList = listMineDataResponse.getData();
            //将数据根据龙然煤矿id转成map
            Map<String, OtherApiJxryNowDto> mineDataMap = new HashMap<>();
            for (OtherApiJxryNowDto dto : mineDataList) {
                mineDataMap.put(dto.getMineID(), dto);
            }

            //获取映射表
            List<MineCodeMapPo> mineCodeMapPos = mineCodeMapMapper.selectList(null);
            Map<String, MineCodeMapPo> mineCodeMap = new HashMap<>();
            for (MineCodeMapPo po : mineCodeMapPos) {
                mineCodeMap.put(po.getMineCode(), po);
            }
            //获取集团煤矿信息
            SysOrganizationVo sysOrganizationVos = RedisUtils.getCacheObject(Constants.SYS_CODESUB_KEY+0);
            if (sysOrganizationVos == null) {
                new HuaNengException(Constants.FAIL, "未获取到华能煤矿信息");
            }
            //获取集团下所有公司
            List<SysOrganizationVo> subCodeList = sysOrganizationVos.getSubCodeList();
            //遍历公司
            List<OtherApiJxryNowVO> OtherApiJxryNowVOAll = new ArrayList<>();
            for (SysOrganizationVo sysOrganizationVo : subCodeList) {
                //遍历煤矿
                List<OtherApiJxryNowVO> collect = sysOrganizationVo.getSubCodeList().stream().map(t -> {
                    OtherApiJxryNowVO otherApiJxryNowVO = new OtherApiJxryNowVO();
                    otherApiJxryNowVO.setMineCode(t.getOrgCode());
                    otherApiJxryNowVO.setMineName(t.getOrgName());
                    //根据获取煤矿编码映射关系
                    if (mineCodeMap.get(t.getOrgCode()) != null) {
                        MineCodeMapPo mineCodeMapPo = mineCodeMap.get(t.getOrgCode());
                        //判断是否有龙软id
                        if (mineCodeMapPo.getMineId() != null) {
                            //获取龙软煤矿id数据
                            OtherApiJxryNowDto otherApiJxryNowDto = mineDataMap.get(mineCodeMapPo.getMineId());
                            //判断龙软数据是否存在
                            if (otherApiJxryNowDto != null) {
                                //有龙软数据
                                otherApiJxryNowVO.setInWellNum(String.valueOf(otherApiJxryNowDto.getInWellNum()));
                                otherApiJxryNowVO.setRemark("数据来自龙软：" + otherApiJxryNowDto.getMineID());
                            } else {
                                //没有龙软数据

                                //获取华能井下人数
                                try {
                                    otherApiJxryNowVO.setInWellNum(remoteLocationService.getPitNumber(t.getOrgCode()));
                                    otherApiJxryNowVO.setRemark("数据来自华能：" + t.getOrgCode());
                                } catch (RuntimeException e) {
                                    otherApiJxryNowVO.setInWellNum("0");
                                    otherApiJxryNowVO.setRemark("fein获取华能数据失败"+e.getMessage());
                                }
                            }
                        } else {
                            //没有龙软id  直接查询华能数据
                            try {
                                otherApiJxryNowVO.setInWellNum(remoteLocationService.getPitNumber(t.getOrgCode()));
                                otherApiJxryNowVO.setRemark("数据来自华能：" + t.getOrgCode());
                            } catch (RuntimeException e) {
                                otherApiJxryNowVO.setInWellNum("0");
                                otherApiJxryNowVO.setRemark("fein获取华能数据失败"+e.getMessage());
                            }
                        }
                    }
                    return otherApiJxryNowVO;
                }).collect(Collectors.toList());
                //将公司下的煤矿汇总返回
                collect.forEach(t -> {
                    OtherApiJxryNowVOAll.add(t);
                });
            }
            return OtherApiJxryNowVOAll;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T sendPostRequest(String url, String appToken, String requestBody, Class<T> responseType) throws IOException {
        // 创建URL对象
        URL apiUrl = new URL(url);

        // 打开HTTP连接
        HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();

        // 设置请求头
        connection.setRequestProperty("appToken", appToken);

        // 设置请求方法为POST
        connection.setRequestMethod("POST");

        // 启用输出流，用于发送请求体数据
        connection.setDoOutput(true);

        // 获取输出流
        OutputStream outputStream = connection.getOutputStream();

        // 将请求体写入输出流
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
        writer.write(requestBody);
        writer.flush();
        writer.close();

        // 获取响应码
        int responseCode = connection.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            // 读取响应数据
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder responseData = new StringBuilder();

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                responseData.append(inputLine);
            }

            // 关闭输入流
            in.close();

            // 将JSON字符串解析为对象
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(responseData.toString(), responseType);
        }

        return null;
    }
    public static <T> T sendGetRequest(String url,String appToken, Class<T> responseType) throws IOException {
        // 创建URL对象
        URL apiUrl = new URL(url);

        // 打开HTTP连接
        HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();

        // 设置请求头
        connection.setRequestProperty("appToken", appToken);

        // 设置请求方法
        connection.setRequestMethod("GET");

        // 获取响应码
        int responseCode = connection.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            // 读取响应数据
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder responseData = new StringBuilder();

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                responseData.append(inputLine);
            }

            // 关闭输入流
            in.close();

            // 将JSON字符串解析为对象
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(responseData.toString(), responseType);
        }
        return null;
    }
    public static <T> T sendGetRequest(String url,String appToken,  TypeReference<T> responseType) throws IOException {
        // 创建URL对象
        URL apiUrl = new URL(url);

        // 打开HTTP连接
        HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();

        // 设置请求头
        connection.setRequestProperty("appToken", appToken);

        connection.setRequestProperty("Content-Type", "application/json");

        // 设置请求方法
        connection.setRequestMethod("GET");

        // 获取响应码
        int responseCode = connection.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK) {
            // 读取响应数据
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder responseData = new StringBuilder();

            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                responseData.append(inputLine);
            }

            // 关闭输入流
            in.close();

            // 将JSON字符串解析为对象
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(responseData.toString(), responseType);
        }
        return null;
    }

    /**
     * 获取扎煤系统signature签证
     */
    @ApiOperation("获取扎煤-signature签证")
    @GetMapping("getSignature")
    public String getSignature(
                                @ApiParam(value = "appKey") String appKey,
                               @ApiParam(value = "appSecret") String appSecret,
                               @ApiParam(value = "timestamp-时间戳") String timestamp)  {

        return otherSystemApiService.getSignature(appKey, appSecret, timestamp);
    }
    /**
     * 获取扎煤系统-采煤每日运行状态总时长
     */
    @ApiOperation("获取扎煤-采煤每日运行状态总时长")
    @GetMapping("getMiningSystemRunTimeDay")
    public List<MiningSystemRunTimeDayDto> getMiningSystemRunTimeDay(
            @ApiParam(value = "ip") String ip,
            @ApiParam(value = "signature") String signature,
            @ApiParam(value = "timestamp") String timestamp,
            @ApiParam(value = "开始时间（YYYY-MM-DD HH:mm:ss）") Date startDate,
            @ApiParam(value = "结束时间（YYYY-MM-DD HH:mm:ss）") Date endDate)  {

        return otherSystemApiService.getMiningSystemRunTimeDay(ip, signature,timestamp, startDate,endDate);

    }
    /**
     * 获取扎煤系统-采煤每分钟运行状态明细
     */
    @ApiOperation("获取扎煤-采煤每分钟运行状态明细")
    @GetMapping("getMiningSystemRunTimeMinuteDetail")
    public List<MiningSystemRunTimeMinuteDetailDto> getMiningSystemRunTimeMinuteDetail(
            @ApiParam(value = "ip") String ip,
            @ApiParam(value = "signature") String signature,
            @ApiParam(value = "timestamp") String timestamp,
            @ApiParam(value = "开始时间（YYYY-MM-DD HH:mm:ss）") Date startDate,
            @ApiParam(value = "结束时间（YYYY-MM-DD HH:mm:ss）") Date endDate)  {

        return otherSystemApiService.getMiningSystemRunTimeMinuteDetail(ip, signature,timestamp, startDate,endDate);
    }

}
